#include<iostream>
const int N = 1e6 + 10;
int ne[N], num[N],n;
using namespace std;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> ne[i];
		num[i] = i;
	}
	int h; cin >> h;
	for (int i = h; i != 0; i = ne[i])
	{
		cout << i << ' ';
	}
	cout << endl;
	return 0;
}